<template>
    <div class="cmt-container">
        <h1>发表评论</h1>
        <hr>
        <textarea placeholder="最多输入200字" maxlength="200" v-model="comtxt"></textarea>
        <mt-button type="primary" size="large" @click="postComment">提交评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in comlist" :key="item.id">
                <div class="cmt-title">
                    第{{i+1}}楼&nbsp;用户:{{item.user}}&nbsp;评论时间:{{item.posttime}}
                </div>
                <div class="cmt-body">
                    {{item.content}}
                </div>
            </div>
        </div>
        <mt-button type="danger" size="large" plain>加载更多</mt-button>
    </div>
</template>
<script>
export default {
    data(){
        return {
            comlist:[],
            comtxt:""
        }
    },
    created(){
        this.getComment()
    },
    methods:{
        getComment(){
            this.comlist=JSON.parse(localStorage.getItem("comt") || "[]")
        },
        postComment(){
            var comdata={id:Date.now(),user:"匿名用户",content:this.comtxt,posttime:new Date()}
            var comts=JSON.parse(localStorage.getItem("comt") || "[]")
            comts.unshift(comdata)
            localStorage.setItem("comt",JSON.stringify(comts))
            this.getComment()
        }

    }
}
</script>

<style lang="scss" scoped>
.cmt-container{
    h1{
        font-size: 18px;
    }
    textarea{
        font-size: 14px;
        height: 85px;
        margin: 0;
    }
    .cmt-list{
        margin-top: 5px;
        .cmt-item{
            font-size: 13px;
            .cmt-title{
                background-color: #ccc;
                line-height: 30px;
            }
            .cmt-body{
                line-height: 35px;
                text-indent: 2em;
            }
        }
    }
}
</style>